package vincent.com.layouttest.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import vincent.com.layouttest.R;

/**
 * Created by zhengwenchen on 2016/12/20.
 */
public class SwitchButton extends RelativeLayout {
    private ImageView thumb;
    private ImageView thumbBg;

    private boolean open = false;

    public SwitchButton(Context context) {
        this(context, null);
    }

    public SwitchButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public boolean isOpen() {
        return open;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    public void setOpen(boolean open) {
        this.open = open;

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)thumb.getLayoutParams();
        if(this.open){
            thumbBg.setImageResource(R.drawable.eq_setting_switch_open_bg);
            params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }else{
            thumbBg.setImageResource(R.drawable.eq_setting_switch_close_bg);
            params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        }

        thumb.setLayoutParams(params);
    }

    public void init(){
        thumb = (ImageView)findViewById(R.id.thumb);
        thumbBg = (ImageView)findViewById(R.id.thumb_bg);
    }
}
